import com.gasstation.user.Role
import com.gasstation.user.User
import com.gasstation.user.UserRole

class DummyData {

	public static void cleanUpAndCreateDummyData() {
		cleanUp()
		createDummyData()
	}

	private static void cleanUp() {
		UserRole.executeUpdate('delete UserRole ur')
		User.executeUpdate('delete User u')
	}

	private static void createDummyData()  {
		def admin = User.findByUsername('admin@gasstation.com')
		if (!admin) {
			admin = new User(username: 'admin@gasstation.com', enabled: true, password: 'password').save()
			Role.list().each {
				UserRole.create admin, it, true
			}
		}

		def member1 = new User(username: 'member1@gasstation.com', enabled: true, password: 'password').save()
		UserRole.create member1, Role.findByAuthority('ROLE_TRACKER'), true
		def member2 = new User(username: 'member2@gasstation.com', enabled: true, password: 'password').save()
		UserRole.create member2, Role.findByAuthority('ROLE_TRACKER'), true
		def projectmanager = new User(username: 'projectmanager@gasstation.com', enabled: true, password: 'password').save()
		UserRole.create projectmanager, Role.findByAuthority('ROLE_TRACKER'), true
		UserRole.create projectmanager, Role.findByAuthority('ROLE_STATISTICS'), true
		UserRole.create projectmanager, Role.findByAuthority('ROLE_CUSTOMER_MANAGER'), true
		UserRole.create projectmanager, Role.findByAuthority('ROLE_AUTOCOMPLETE_MANAGER'), true

		def today = new Date()
		today.putAt(Calendar.HOUR_OF_DAY, 9)
		today.putAt(Calendar.MINUTE, 0)
		def beforeTenDays = today-10
	}
}
